
package cn.org.seventh.common;

import java.io.Serializable;
import java.util.Map;

/**
 * @author <a href="mailto:ji.li@symbio-group.com">Andy</a>
 * @since 2007-11-29 下午10:37:43
 */
public class Selector implements Serializable {

    private static final long serialVersionUID = -850468046227578258L;

    private Integer firstResult;

    private Integer maxResults;

    public Integer getFirstResult() {
        return firstResult;
    }

    public void setFirstResult(Integer firstResult) {
        this.firstResult = firstResult;
    }

    public Integer getMaxResults() {
        return maxResults;
    }

    public void setMaxResults(Integer maxResults) {
        this.maxResults = maxResults;
    }

    public int hashCode() {
        final int PRIME = 31;
        int result = 1;
        result =
            PRIME * result
                + ((firstResult == null) ? 0 : firstResult.hashCode());
        result =
            PRIME * result + ((maxResults == null) ? 0 : maxResults.hashCode());
        return result;
    }

    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        final Selector other = (Selector) obj;
        if (firstResult == null) {
            if (other.firstResult != null)
                return false;
        } else if (!firstResult.equals(other.firstResult))
            return false;
        if (maxResults == null) {
            if (other.maxResults != null)
                return false;
        } else if (!maxResults.equals(other.maxResults))
            return false;
        return true;
    }

    /**
     * @param model
     * @param currentPage
     * @param perPageSize
     * @param totleRecordsSize
     * @return
     */
    public static Selector splitPage(Map model, String currentPage,
                                     Integer perPageSize, Long totleRecordsSize) {
        long perPage = 1;
        perPage = perPageSize.intValue();
        long totleRecords = 0;
        totleRecords = totleRecordsSize.intValue();
        long totlePages = 1;
        long page = 1;
        if (totleRecords > 0)
            totlePages =
                (totleRecords / perPage) + (totleRecords % perPage > 0 ? 1 : 0);
        try {
            page = Integer.parseInt(currentPage);
            page = page < 1 ? 1 : page;
            page = page > totlePages ? totlePages : page;
        } catch (Exception e) {
            page = 1;
        }
        long firstPage = 1;
        long lastPage = totlePages;
        long prePage = (page - 1) > 0 ? (page - 1) : 1;
        long nextPage = (page + 1) > totlePages ? totlePages : (page + 1);
        model.put("firstPage", new Long(firstPage));
        model.put("lastPage", new Long(lastPage));
        model.put("prePage", new Long(prePage));
        model.put("nextPage", new Long(nextPage));
        model.put("totlePages", new Long(totlePages));
        model.put("totleRecords", new Long(totleRecords));
        model.put("perPage", new Long(perPage));
        model.put("page", new Long(page));
        Integer firstResult = new Integer((int) ((page - 1) * perPage));
        Integer maxResults = new Integer((int) perPage);
        Selector selector = new Selector();
        selector.setFirstResult(firstResult);
        selector.setMaxResults(maxResults);
        return selector;
    }

    /**
     * used to generate hibernate hql paramiter only when using like
     * 
     * @param str
     * @return
     */
    public static String like(String str) {
        return "%" + str + "%";
    }

    public static boolean isNotEmptyString(String str) {
        if (null != str && !"".equals(str.trim())) {
            return true;
        } else {
            return false;
        }
    }
}
